﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.EventSystems;

public enum VIPNetType {
    None,
    WiFi,
    Data
}

public class VIPUtil {

    /// <summary>
    /// UGUI判断手指是否触摸在了UI上(兼容iPhone,Android,PC)
    /// </summary>
    /// <returns>是/否</returns>
    public static bool IsUI()
    {
        bool bo = true;
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
#if IPHONE || ANDROID
			if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
            if (EventSystem.current.IsPointerOverGameObject())
#endif
                bo = true;
            else
                bo = false;
        }
        return bo;
    }

    /// <summary>
    /// 判断是否有网络
    /// </summary>
    /// <returns></returns>
    public static bool IsNet()
    {
        bool bo = true;
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            bo = false;
        }
        return bo;
    }

    /// <summary>
    /// 判断是什么网络
    /// </summary>
    /// <returns></returns>
    public static VIPNetType IsNetType() {
        //当用户使用WiFi时  
        if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
        {
            return VIPNetType.WiFi;
        }
        //当用户使用移动网络时
        if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
        {
            return VIPNetType.Data;         
        }
        return VIPNetType.None;
    }

    /// <summary>
    /// 获取当前选中的游戏对象(依赖EventSystem事件)
    /// </summary>
    /// <returns></returns>
    public static GameObject GetCurrentSelectedGameObject()
    {
        return EventSystem.current.currentSelectedGameObject;
    }

    /// <summary>
    /// 获取本机IP地址
    /// </summary>
    /// <returns>本机IP地址</returns>
    public static string GetIP()
    {
        try
        {
            string HostName = Dns.GetHostName(); //得到主机名
            IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
            for (int i = 0; i < IpEntry.AddressList.Length; i++)
            {
                //从IP地址列表中筛选出IPv4类型的IP地址
                //AddressFamily.InterNetwork表示此IP为IPv4,
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    return IpEntry.AddressList[i].ToString();
                }
            }
            return "";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    /// <summary>
    /// 通过NetworkInterface获取MAC地址
    /// </summary>
    /// <returns></returns>
    public static string GetMacAddress(bool bar = false)
    {
        string macStr = "";
        try
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                if (bar)
                {
                    macStr = BitConverter.ToString(ni.GetPhysicalAddress().GetAddressBytes());
                }
                else
                {
                    macStr = ni.GetPhysicalAddress().ToString();
                }
                return macStr;
            }
        }
        catch (Exception)
        {
            return null;
        }
        if (bar)
        {
            macStr = "00-00-00-00-00-00";
        }
        else
        {
            macStr = "000000000000";
        }
        return macStr;
    }

}
